<?php

class VideoController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $this->view->categoryID = CATE_ID_VIDEO;

        //init Model
        $cateModel = Category::getInstance();
        $newsModel = News::getInstance();

        /* ============================= START TOP+HOT ======================== */

        $intVideoID        = $this->getRequest()->getParam('videoid', 0);
        //limit top+hot
        $limitTopHot       = LIMIT_HOT_VIDEO_SITE + 1; //(1:top)
        //get video noi bat
        $arrNoiBat         = $newsModel->getListTopArticle(CATE_ID_VIDEO, $limitTopHot, 0, VIDEO);
        $arrNoiBat["data"] = (array) $arrNoiBat["data"];


        $videoInfo = array();
        $arrHotVideoResult = array();
        if ($intVideoID === 0)
        {
            //get detail article list
            $arrArticleInfo = $newsModel->getDetailArticleByIDs($arrNoiBat["data"]);
            if (!empty($arrArticleInfo))
            {
                //top 1
                $videoInfo         = array_splice($arrArticleInfo, 0, 1);
                $videoInfo         = $videoInfo[0];
                //hot list
                $arrHotVideoResult = $arrArticleInfo;
            }
            //arr excluse
            $arrVideoTopExclu  = $arrNoiBat["data"];
        }
        else
        {
            //get detail video
            $videoInfo = $newsModel->getDetailArticleByIDs(array($intVideoID));

            if (!empty($videoInfo))
            {
                $videoInfo         = $videoInfo[$intVideoID];
            }
            //loai tru video detail khoi list noi bat
            $arrNoiBat["data"] = array_diff($arrNoiBat["data"], array($intVideoID));
            //get hot video list
            $arrNoiBat["data"] = array_splice($arrNoiBat["data"], 0, LIMIT_HOT_VIDEO_SITE);
            $arrHotVideoResult = $newsModel->getDetailArticleByIDs($arrNoiBat["data"]);
            //arr excluse
            $arrVideoTopExclu  = array_merge($arrNoiBat["data"], array($intVideoID));
        }

        $this->view->videoDetail = $videoInfo;
        $this->view->arrHotVideoResult = $arrHotVideoResult;
        unset($arrNoiBat);
        /* ============================= END ======================== */

        /* ================ GET LIST MEDIA LIEN QUAN  =============== */
        $cateID         = ( $intVideoID > 0 && !empty($videoInfo)) ? $videoInfo["category_id"] : CATE_ID_VIDEO;
        $arrMediaResult = array();
        if (!empty($videoInfo))
        {
            //get list tag by article
            $arrTag = $newsModel->getTagsByArticleID($videoInfo["article_id"]);

            if (!empty($arrTag))
            {
                $totalTag = count($arrTag);
                for ($i        = 0; $i < $totalTag; $i++)
                {
                    //get list article by tag
                    $arrMedia = $newsModel->getListArticleByTag($arrTag[$i], LIMIT_RELATED_MEDIA, 0, VIDEO);

                    if ($arrMedia["total"] > 0)
                    {
                        $arrMediaResult = array_merge($arrMediaResult, $arrMedia["data"]);
                        //excluse current video play
                        $arrMediaResult = array_diff(array_unique($arrMediaResult), $arrVideoTopExclu);
                    }
                    if ($arrMedia["total"] <= 0 && count($arrMediaResult) < LIMIT_RELATED_MEDIA)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //toltal media
            $totalMedia = count($arrMediaResult);
            //if total media < 2 then get new video
            if ($totalMedia < LIMIT_RELATED_MEDIA)
            {
                $limit_more  = (LIMIT_RELATED_MEDIA - $totalMedia) + $limitTopHot; //( $limitTopHot là total top & hot video)
                //get list articleid new
                $arrNewVideo = $newsModel->getListTopArticle($cateID, $limit_more, 0, VIDEO);
                if (!empty($arrNewVideo["data"]))
                {
                    //merge media & new video
                    $arrMediaResult = array_merge($arrMediaResult, $arrNewVideo["data"]);
                }
            }
            //get article detail list
            if (!empty($arrMediaResult))
            {
                //excluse video top+hot
                $arrMediaResult   = array_diff(array_unique($arrMediaResult), $arrVideoTopExclu);
                $arrMediaResult   = array_splice($arrMediaResult, 0, LIMIT_RELATED_MEDIA);
                // merge excluse to get new video list
                $arrVideoTopExclu = array_merge($arrVideoTopExclu, $arrMediaResult);
                // ge detail
                $arrMediaResult   = $newsModel->getDetailArticleByIDs($arrMediaResult);
            }
        }
        //assign data
        $this->view->arrMediaResult = $arrMediaResult;
        /* ============================= END ======================== */

        /* ============== START GET LIST NEW VIDEO LIST ============== */
        //get list article id by category's id
        $arrNewVideoResult = $newsModel->getListTopArticle($cateID, LIMIT_NEW_VIDEO_SITE, 0, VIDEO);

        // Get total video
        $totalVideo   = $arrNewVideoResult['total'] - $arrNewVideoResult['excluse']['total'];
        $arrVideoData = array();
        if ($totalVideo)
        {
            // Get video detail by ids
            $arrVideoData = $newsModel->getDetailArticleByIDs($arrNewVideoResult['data']);
        }

        $cateDetail = $cateModel->getDetailbyCateId($cateID);

        // Assign to view
        $this->view->assign(array(
            'cateID'       => $cateID,
            'cateName'     => strtolower($cateDetail['catename']) == 'video' ? 'Thể thao' : $cateDetail['catename'],
            'totalVideo'   => $totalVideo,
            'totalPage'    => $totalVideo ? intval(ceil($totalVideo / LIMIT_NEW_VIDEO_SITE)) : 0,
            'currPage'     => 1,
            'arrVideoData' => $arrVideoData,
            'excluse'      => Zend_Json::encode($arrNewVideoResult['excluse']['data']),
        ));

        /* ============== END GET LIST NEW VIDEO LIST ============== */
        /* ============= Get list cate video ============== */
        $this->view->arrMenuVideo = $cateModel->getMenu(CATE_ID_VIDEO);

        // SEO
        if ($intVideoID == 0)
        {
            $this->view->headTitle('Xem video clip, bóng đá, bàn thắng đẹp, pha ghi bàn 2012 - VnExpress');
        }
        else
        {
            $this->view->headTitle('Xem video clip ' . $videoInfo['title'] . ' - VnExpress');
        }

        $this->view->headMeta()->setName('description', $videoInfo['lead']);
        $this->view->headMeta()->setName('keywords', 'Video,' . (!empty($strCategoryname) ? $strCategoryname . ',' : '') . ' online');
    }

    /*
     * Get data video for xml
     */

    function vneInfoAction()
    {
        $this->_helper->layout()->disableLayout();
        $intVideoID = (int) $this->getRequest()->getParam('id', 0);

        if ($intVideoID > 0 && $intVideoID < 5000000)
        {
            // Get detail article
            $newsModel  = News::getInstance();
            $arrArticle = $newsModel->getDetailArticleByIDs(array($intVideoID));
            $arrMedia = array();
            if (!empty($arrArticle))
            {
                // Reference article
                $arrMedia   = $newsModel->getObjectReferenceByArticleID($intVideoID, 1, 0);
                $arrArticle = $arrArticle[$intVideoID];
                if (!empty($arrMedia))
                {
                    $arrMedia = current($arrMedia);
                }
            }
            $this->view->arrMedia = $arrMedia;
            $this->view->arrArticle = $arrArticle;
        }
        $this->view->videoID = $intVideoID;
    }

    function vneMoreAction()
    {
        $this->_helper->layout()->disableLayout();
    }

    public function loadListVideoAction()
    {
        // Disable render layout
        $this->_helper->layout->disableLayout(true);
        $this->_helper->viewRenderer->setNoRender(true);

        // Default response
        $response = array(
            'error' => 0,
            'msg'   => 'Success',
            'html'  => NULL,
            'ext'   => NULL
        );

        // Get params
        $cateID     = intval($this->_request->getParam('cateID', 0));
        $currPage   = intval($this->_request->getParam('currPage', 1));
        $arrExcluse = $this->_request->getParam('excluse', NULL);

        // Get model's instance
        $newsModel = News::getInstance();

        // if hasn't cate => load by id
        if ($cateID)
        {
            
            $cateModel = Category::getInstance();

            // Get cate's detail
            $cateDetail = $cateModel->getDetailbyCateId($cateID);

            //get list article id by category's id
            $arrNewVideoResult = $newsModel->getListTopArticle($cateID, LIMIT_NEW_VIDEO_SITE, ($currPage - 1) * LIMIT_NEW_VIDEO_SITE, VIDEO, array('total' => count($arrExcluse), 'data'  => $arrExcluse));

            $totalVideo   = $arrNewVideoResult['total'];// - $arrNewVideoResult['excluse']['total'];
            $totalPage = $totalVideo ? intval(ceil($totalVideo / LIMIT_NEW_VIDEO_SITE)) : 0;
            $arrVideoData = array();
            if ($totalVideo)
            {
                // Get video detail by ids
                $arrVideoData = $newsModel->getDetailArticleByIDs($arrNewVideoResult['data']);
            }

            // Assign to view
            $this->view->assign(array(
                'cateID'       => $cateID,
                'cateName'     => strtolower($cateDetail['catename']) == 'video' ? 'Thể thao' : $cateDetail['catename'],
                'totalVideo'   => $totalVideo,
                'totalPage'    => $totalVideo ? intval(ceil($totalVideo / LIMIT_NEW_VIDEO_SITE)) : 0,
                'currPage'     => 1,
                'arrVideoData' => $arrVideoData,
                'excluse'      => Zend_Json::encode($arrNewVideoResult['excluse']['data']),
            ));

            // Assign to view
            $this->view->assign(array(
                'cateName'     => strtolower($cateDetail['catename']) == 'video' ? 'Thể thao' : $cateDetail['catename'],
                'totalVideo'   => $totalVideo,
                'totalPage'    => $totalPage,
                'currPage'     => $currPage,
                'arrVideoData' => $arrVideoData,
            ));

            $response['html'] = $this->view->render('video/box/box_list_video.phtml');
            $response['ext']  = array(
                'currPage'     => $currPage,
                'totalVideo'   => $totalVideo,
                'totalPage'    => $totalPage,
            );
        }

        echo Zend_Json::encode($response);

        exit;
    }

}